﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicDemo.ch8
{
    internal class MapDemo
    {
        public void ShowMap()
        {
            Dictionary<string, string> personMap = new Dictionary<string, string>()
            {
                ["name"] = "huyucai",
                ["gender"] = "male",
                ["gender"] = "female"
            };
            personMap.Add("age", "30");
            //personMap.Add("age", "30");//此行报错，不能添加重复的key

            foreach (KeyValuePair<string,string> item in personMap)
            {
                Console.WriteLine($"key={item.Key},value={item.Value}");
            }

            foreach (string item in personMap.Keys)
            {
                Console.WriteLine(item);
            }
            foreach (string item in personMap.Values)
            {
                Console.WriteLine(item);
            }

            bool exist = personMap.TryGetValue("age1", out string age);
            Console.WriteLine(exist);
            Console.WriteLine(age);

            string agestr = personMap.ContainsKey("age") ? personMap["age"] : null;
            Console.WriteLine(agestr);
        }
    }
}
